#include "tppmwidget.h"
#include "ui_tppmwidget.h"
#include "mainwindow.h"
#include "projectdatabaseoperations.h"

extern int uniqueDeviceID;

TPPMWidget::TPPMWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TPPMWidget)
{
    ui->setupUi(this);
    
    error = new ErrorToDo;
    
    if(initDataFromProjectDB() < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"从数据库载入接口对象表参数失败";
        error->showCriticalMsg(this, "从数据库载入接口对象表参数失败");
        return;
    }
    
    connect(ui->ModelOnoffBox, SIGNAL(valueChanged(int)), this, SLOT(updateModelOnoff(int)));
    connect(ui->PMAlarmValueBox, SIGNAL(valueChanged(int)), this, SLOT(updatePMAlarmValue(int)));
    connect(ui->CO2AlarmValueBox, SIGNAL(valueChanged(int)), this, SLOT(updateCO2AlarmValue(int)));
    connect(ui->HumRhAlarmValueBox, SIGNAL(valueChanged(int)), this, SLOT(updateHumRhAlarmValue(int)));
    connect(ui->UIParameterGetBox, SIGNAL(valueChanged(int)), this, SLOT(updateUIParameterGetValue(int)));
    connect(ui->BacklitDelayBox, SIGNAL(valueChanged(int)), this, SLOT(updateBacklitDelay(int)));
}

TPPMWidget::~TPPMWidget()
{
    delete error;
    delete ui;
}

int TPPMWidget::initDataFromProjectDB()
{
    int ModelOnoff = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 0);
    if(ModelOnoff < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取模块开闭数据库value值失败";
        error->showCriticalMsg(this, "获取模块开闭数据库value值失败");
        return ModelOnoff;
    }
    ui->ModelOnoffBox->setValue(ModelOnoff);
    
    int PMAlarmValue = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 1);
    if(PMAlarmValue < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取PM2.5报警值数据库value值失败";
        error->showCriticalMsg(this, "获取PM2.5报警值数据库value值失败");
        return PMAlarmValue;
    }
    ui->PMAlarmValueBox->setValue(PMAlarmValue);
    
    int CO2AlarmValue = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 2);
    if(CO2AlarmValue < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取CO2报警值数据库value值失败";
        error->showCriticalMsg(this, "获取CO2报警值数据库value值失败");
        return CO2AlarmValue;
    }
    ui->CO2AlarmValueBox->setValue(CO2AlarmValue);
    
    int HumRhAlarmValue = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 3);
    if(HumRhAlarmValue < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取湿度报警值数据库value值失败";
        error->showCriticalMsg(this, "获取湿度报警值数据库value值失败");
        return HumRhAlarmValue;
    }
    ui->HumRhAlarmValueBox->setValue(HumRhAlarmValue);
    
    int UIParameterGetValue = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 4);
    if(UIParameterGetValue < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取界面参数定时采集数据库value值失败";
        error->showCriticalMsg(this, "获取界面参数定时采集数据库value值失败");
        return UIParameterGetValue;
    }
    ui->UIParameterGetBox->setValue(UIParameterGetValue);
    
    int BacklitDelay = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 5);
    if(BacklitDelay < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取背光延时数据库value值失败";
        error->showCriticalMsg(this, "获取背光延时数据库value值失败");
        return BacklitDelay;
    }
    ui->BacklitDelayBox->setValue(BacklitDelay);
    
    return 1;
}

void TPPMWidget::updateModelOnoff(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value<<"设备id："<<uniqueDeviceID;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 0, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void TPPMWidget::updatePMAlarmValue(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 1, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void TPPMWidget::updateCO2AlarmValue(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 2, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void TPPMWidget::updateHumRhAlarmValue(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 3, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void TPPMWidget::updateUIParameterGetValue(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 4, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void TPPMWidget::updateBacklitDelay(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 5, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}
